const app = getApp();
const util = require('../../utils/util.js')
Page({
    data: {
        equipmentTracks: [],
        imgUrl: app.globalData.imgUrl,
        equipmentId: '',
        equipmentImei: '',
        equipRecord: {},
        productList: [],
        priceInputDisabled: true,
        equipmentType:""
    },
    onLoad: function (option) {
        wx.setNavigationBarTitle({
            title: '设备详情'
        })
        this.setData({
            equipmentId: option.id,
            equipmentImei: option.imei,
            equipmentType: option.type,
        })
        console.log(this.data.equipmentId, this.data.equipmentImei);
        this.getEquipmentInfo(this.data.equipmentId, this.data.equipmentImei);
    },
    reload: function () {
        this.getEquipmentInfo(this.data.equipmentId, this.data.equipmentImei);
    },

    getEquipmentInfo: function (id, imei) {
        var that = this;
        wx.showLoading({
            title: '加载中'
        })
        var param = {
            token: app.globalData.token,
            equipment_id: id,
            imei: imei
        };
        for (var i in param) {
            if (param[i] == '') {
                delete param[i]
            }
        }
        console.log(param);
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/getEquipDetail',
            data: param,
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading()
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        equipRecord: res.data.equipRecord,
                        equipmentId: res.data.equipRecord.id,
                        productList: res.data.productList,
                        equipmentTracks: res.data.equipmentTracks
                    })
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })

    }
    ,
    replenFull: function () {
        console.log(this.data.equipmentTracks[0].stock)
    },
    stepperValue: function (e) {
        //调整本地数据
        var id = e.currentTarget.id;
        var value = e.detail.value
        var param = {};
        for (var i = 0; i < this.data.equipmentTracks.length; i++) {
            if (this.data.equipmentTracks[i].id == id) {
                var string = "equipmentTracks[" + i + "].stock";
                param[string] = value;
                this.setData(param)
            }
        }
    },
    openLock: function () {
        wx.showLoading({
            title: '开锁中',
        });
        var id = this.data.equipmentId;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/openLock',
            data: {
                token: app.globalData.token,
                equipment_id: id,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showModal({
                        title: '开锁成功',
                        content: '开锁指令发送成功，请耐心等待',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            }
                        }
                    });
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    adjustTrack: function (e) {
        console.log(e);
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        var track = e.target.id;
        var id = this.data.equipmentId;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/getProductEachTrack',
            data: {
                token: app.globalData.token,
                equipment_id: id,
                track_id: track,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    var productName = [];
                    var productId = [];
                    res.data.cargoProducts.forEach(function (item, i) {
                        productName.push(item.productname);
                        productId.push(item.product_id);
                    });
                    wx.showActionSheet({
                        itemList: productName,
                        success: function (res) {
                            if (!res.cancel) {
                                console.log(res.tapIndex)
                                //修改货道商品显示
                                var index = res.tapIndex;
                                that.changeProduct(track, productId[index], productName[index])

                            }
                        }
                    });
                }

            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    changeProduct: function (trackId, productId, productName) {
        var that = this;
        var id = this.data.equipmentId;
        console.log(trackId, productId, productName)
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/changeProduct',
            data: {
                token: app.globalData.token,
                equipment_id: id,
                track_id: trackId,
                product_id: productId
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();

                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '调整成功',
                        icon: 'success',
                        duration: 1000
                    });
                    var param = {};
                    for (var i = 0; i < that.data.equipmentTracks.length; i++) {
                        if (that.data.equipmentTracks[i].id == trackId) {
                            var editTrack = "equipmentTracks[" + i + "].product_name";
                            param[editTrack] = productName;
                        }
                    }
                    console.log(param)
                    that.setData(param)
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    dropTest: util.throttle(function (e) {
        console.log(e)
        var id = this.data.equipmentId;
        var track = e.target.id;
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/testOne',
            data: {
                token: app.globalData.token,
                track_id: track,
                equipment_id: id,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                console.log(res.data)
                wx.hideLoading();
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showModal({
                        title: '指令已发送，检查掉落',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                that.reload();
                            }
                        }
                    });
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    }, 1000),
    replenFull:

        function () {
            var that = this;
            var id = this.data.equipmentId;
            wx.request({
                url: app.globalData.APIURL + '/vending-admin/operator/fillAll',
                data: {
                    token: app.globalData.token,
                    equipment_id: id,
                },
                // method:'GET',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    wx.hideLoading();

                    console.log(res.data)
                    if (app.requestEro(res.data.success, res.data.msg)) {
                        wx.showModal({
                            title: '补货完成',
                            content: '点击确定返回补货列表',
                            success: function (res) {
                                console.log(res);
                                if (res.confirm) {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                } else {
                                    that.reload();
                                    console.log('用户点击取消')
                                }
                            }
                        })
                    }
                },
                fail: function (err) {
                    console.log(err)
                }
            })
        }

    ,
    replenFinish: function () {
        var that = this;
        var id = this.data.equipmentId;
        var param = {equipmentTracks: []};
        var equipmentTracks = []
        this.data.equipmentTracks.forEach(function (item, i) {
            param.equipmentTracks.push({
                id: item.id,
                product_id: item.product_id,
                stock: item.stock
            })
        });
        console.log(param)

        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/multiFill',
            data: {
                token: app.globalData.token,
                equipment_id: id,
                paras: param,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();

                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showModal({
                        title: '补货完成',
                        content: '点击确定返回补货列表',
                        success: function (res) {
                            console.log(res);
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                })
                            } else {
                                console.log('用户点击取消')
                                // that.reload();
                            }
                        }
                    })
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    editPrice: function () {
        this.setData({
            priceInputDisabled: false,
        })
    },
    cancelSavePrice: function () {
        this.setData({
            priceInputDisabled: true,
        })
    },
    setPrice: function (e) {
        console.log(e)
        var price = e.detail.value;
        var index = e.currentTarget.dataset.name;
        var param = {}, productPrice = 'productList[' + index + '].price';
        param[productPrice] = price;
        this.setData(param)
        console.log(this.data.productList)
    },
    savePrice: function () {
        wx.showLoading({
            title:'正在保存'
        });
        var that = this;
        var equipmentId = this.data.equipmentId;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/batchSetPrice',
            data: {
                token: app.globalData.token,
                equip_id: equipmentId,
                productList: that.data.productList,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showModal({
                        title:'保存成功',
                        showCancel:false,
                        success: function (res) {
                            if (res.confirm) {
                                that.setData({
                                    priceInputDisabled: true,
                                })
                            }
                        }
                    })

                }

            },
            fail: function (err) {
                console.log(err)
            }
        })
    }

});